Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser endpoint #1

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Parser endpoint #1

wants to merge 23 commits into from

Conversation

mitchdowney
Copy link
Member

No description provided.


export async function handler(ctx: Koa.BaseContext, next: () => Promise<void>) {
const { query } = ctx
// TODO: remove any?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES REMOVE, there should be no any's in this codebase. Use zod(https://zod.dev) to get a proper type around query.

Hint: create a zod type (which can also generate typescript type) and then call .parse(query)

Comment on lines 40 to 56
// helpers in alphabetical order

const addCacheBustUrlParameter = (url: string, excludeCacheBust?: boolean) => {
return !excludeCacheBust
? addParameterToURL(url, `cacheBust=${Date.now()}`)
: url
}

const createAbortController = () => {
const abortTimeLimit = 60000
const abortController = new AbortController()
const abortTimeout = setTimeout(() => {
logPerformance('abortController - time exceeded')
abortController.abort()
}, abortTimeLimit)
return { abortController, abortTimeout }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move these out to a separate file (./helpers.ts in this same folder is fine), I generally get very concerned when files are hundreds of lines long (I have a weird thing where I think everything should be less than 150 lines)

You're establishing a pattern here that's just going to make the file huge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Tbh I was planning on moving all of these out, but thought I'd ask for your guidance on how to best organize these.

Comment on lines 53 to 171
split: r.split.toString(),
fee: r.fee,
customKey: r.customKey,
customValue: r.customValue
}
}),
// TODO: get rid of / resolve valueTimeSplits type issue?
valueTimeSplits: val.valueTimeSplits as any || []
}
}

const podcastCompat = (feed: FeedObject) => {
return {
author: Array.isArray(feed.author) ? feed.author : feed.author ? [feed.author] : [],
blocked: feed.itunesBlock,
categories: feed.itunesCategory,
description: feed.description,
explicit: feed.explicit,
funding: Array.isArray(feed.podcastFunding) ? feed.podcastFunding?.map((f) => fundingCompat(f)) : [],
generator: feed.generator,
guid: feed.guid,
imageURL: feed.itunesImage || feed.image?.url,
itunesType: feed.itunesType || podcastItunesTypeDefaultValue,
language: feed.language,
lastBuildDate: feed.lastBuildDate,
link: feed.link,
liveItems: feed.podcastLiveItems ?? [],
medium: feed.medium ?? Phase4Medium.Podcast,
owner: feed.owner,
pubDate: feed.pubDate,
subtitle: feed.subtitle,
summary: feed.summary,
title: feed.title,
type: feed.itunesType,
value: feed.value ? [valueCompat(feed.value)] : []
}
}

// Convert the podcast-partytime schema to a podverse compatible schema.
const episodeCompat = (episode: Episode) => {
return {
alternateEnclosures: episode.alternativeEnclosures ?? [],
author: [episode.author],
chapters: episode.podcastChapters,
// TODO: why does contentLinks exist on liveItem but not episode type?
contentLinks: episode.contentLinks || [],
description: episode.content || episode.description,
duration: episode.duration,
enclosure: episode.enclosure,
explicit: episode.explicit,
// funding: Array.isArray(episode.podcastFunding) ? episode.podcastFunding?.map((f) => fundingCompat(f)) : [],
guid: episode.guid,
imageURL: episode.image,
itunesEpisode: episode.podcastEpisode?.number || episode.itunesEpisode,
itunesEpisodeType: episode.itunesEpisodeType,
itunesSeason: episode.podcastSeason?.number || episode.itunesSeason,
link: episode.link,
pubDate: episode.pubDate,
socialInteraction: episode.podcastSocialInteraction ?? [],
soundbite: episode.podcastSoundbites ?? [],
subtitle: episode.subtitle,
summary: getLongerSummary(episode.content, episode.description),
title: episode.title,
transcript: episode.podcastTranscripts ?? [],
value: episode.value ? [valueCompat(episode.value)] : []
} as ParsedEpisode
}

// TODO: there is duplication here with episodeCompat...can we reuse?
const liveItemCompatToEpisode = (liveItem: ExtendedPhase4PodcastLiveItem) => {
const getChatEmbedUrlValue = (chat?: ExtendedChat) => {
if (chat?.phase === 'pending' && chat.embedUrl) {
return chat.embedUrl
}
// deprecated embed value
else if (chat?.phase === '4' && chat.url) {
return chat.url
}
return ''
}

return {
chat: getChatEmbedUrlValue(liveItem.chat),
end: liveItem.end,
episode: episodeCompat({
...liveItem,
contentLinks: liveItem.contentLinks, // TODO:
duration: 0,
explicit: false, // TODO: liveItem.explicit
socialInteraction: [],
soundbite: [],
subtitle: '', // TODO: liveItem.subtitle
summary: '', // TODO: liveItem.summary,
transcript: []
}),
start: liveItem.start,
status: liveItem.status?.toLowerCase()
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, I'd create a ./compat.ts file or something. This will force you to export them and then if you ever want to, they're easier to test.

@@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "node --import tsx -r dotenv/config src/dev.ts",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I'd leave dev alone and add a dev:watch command with your changes, but you do you 😃

@mitchdowney mitchdowney marked this pull request as ready for review December 18, 2023 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants